package fr.emn.integration.synthese.abstraction;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class ListeEleve {
	private BDCours DB;
	private static Connection connexion;

	private ArrayList<Eleve> listEleve;

	private Cours c;

	public ListeEleve(BDCours DB, Cours c) {
		this.DB = DB;
		this.c = c;
		DB.connexionBD();
		connexion = DB.getConnexion();
		this.listEleve = new ArrayList<Eleve>();
	}

	public void resetList() {
		this.listEleve.clear();
	}

	@SuppressWarnings("resource")
	public void initList() {
		try {
			ResultSet res = null;
			Statement s = connexion.createStatement();
			switch (c.getModuleCourante()) {
			case -1:
				switch (c.getGroupeCourante()) {
				case -1:
					switch (c.getEvaluationCourante()) {
					case -1:
						res = s.executeQuery("SELECT id_e FROM Eleve");
						break;
					default:
						res = s.executeQuery("SELECT id_e FROM note WHERE id_eval="
								+ c.getEvaluationCourante());
						break;
					}break;
				default:
					switch (c.getEvaluationCourante()) {
					case -1:
						res = s.executeQuery("SELECT id_e FROM Relation"
								+ " WHERE id_g="
								+ c.getGroupeCourante());
						break;
					default:
						res = s.executeQuery("SELECT id_e FROM note INNER JOIN relation ON (note.id_e = relation.id_e) "
								+ "WHERE id_g="
								+ c.getGroupeCourante()
								+ "AND id_eval=" + c.getEvaluationCourante());
						break;
					}break;
				}break;
			default:
				switch (c.getGroupeCourante()) {
				case -1:
					switch (c.getEvaluationCourante()) {
					case -1:
						res = s.executeQuery("SELECT id_e FROM relation INNER JOIN Groupe ON (relation.id_g=Groupe.id_g) "
								+ "WHERE groupe.id_M=" + c.getModuleCourante());
						break;
					default:
						res = s.executeQuery("SELECT id_e FROM note INNER JOIN evaluation ON (evaluation.id_eval=note.id_eval) "
								+ "WHERE id_M=" + c.getModuleCourante()
								+ "AND id_eval="
								+ c.getEvaluationCourante());
						break;
					} break;
				default:
					switch (c.getEvaluationCourante()) {
					case -1:
						res = s.executeQuery("SELECT id_e FROM relation INNER JOIN Groupe ON (relation.id_g=Groupe.id_g) "
								+ "WHERE Groupe.id_M=" + c.getModuleCourante() +"AND relation.id_g="+ c.getGroupeCourante());
						break;
					default:
						res = s.executeQuery("SELECT id_e FROM note INNER JOIN evaluation ON (evaluation.id_eval=note.id_eval)" +
								" INNER JOIN relation ON (relation.id_e=note.id_e)"
								+ "WHERE evaluation.id_M=" + c.getModuleCourante()
								+ " AND id_g="
								+ c.getGroupeCourante()
								+ "AND id_eval="
								+ c.getEvaluationCourante());
						break;
					}break;
				}break;
			}
			if(res != null){
				addList(res);
			}
			

		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void addList(ResultSet res) {
		if (res != null) {
			
			try {
				while (res.next()) {
					listEleve.add(new Eleve(this.DB, res.getInt("id_e")));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public void updateList() {
		this.resetList();
		this.initList();
	}
	
	public ArrayList<Eleve> getList(){
		return this.listEleve;
	}

}
